package team2.gbike.location;

import bas.gps_fiets.RouteSelector;
import android.location.Location;
import android.util.Log;
import team2.gbike.main.GBike;
import team2.gbike.map.AndroidGoogleMapsActivity;

public class GPSStore {

	static GPSStore instance;
	static public GBike firstActivity;
	static public RouteSelector placesOfInterestActivity = null;
	static public AndroidGoogleMapsActivity mapViewActivity = null;
	static private Location oldLocation = null;
	static private int attempts = 0;

	private GPSStore() {
	}

	public static GPSStore getInstance() {
		if (instance == null) {

			Log.e("GPSStore", "init");
			instance = new GPSStore();
		}
		return instance;
	}

	boolean debugVariableEraseInTheFuture = false;

	public void setLocation(Location location) {
		
		if (location != null) {
			if(oldLocation == null){
				oldLocation = location;
			}
			if(location.equals(oldLocation)){
				attempts++;
				if(attempts==10){
					GBike.communicator.sendBTMsg("x");
				}
			} else {
				attempts =0;
				GBike.communicator.sendBTMsg("c");
			}
			
			
			AndroidGoogleMapsActivity.gpsLocation = location;
			AndroidGoogleMapsActivity.isLocationAvaliable = true;
			if (placesOfInterestActivity != null) {
				if (placesOfInterestActivity.statusGPS != null
						&& placesOfInterestActivity.messageGPS != null
						//&& placesOfInterestActivity.messageGPS2 != null) {
						){
					placesOfInterestActivity.statusGPS
							.setText("GPS Connected!");
					placesOfInterestActivity.messageGPS.setText("Let's tour!");
					//placesOfInterestActivity.messageGPS2.setText("");
					placesOfInterestActivity.progressGPS
							.setProgress(placesOfInterestActivity.progressGPS
									.getMax());
				}
			}
			if (mapViewActivity != null) {
			
				AndroidGoogleMapsActivity.showLocationToast = true;

				if (debugVariableEraseInTheFuture == false) {
					mapViewActivity.updateRoute();
					debugVariableEraseInTheFuture = true;
				} else {
					mapViewActivity.step();
				}

			}
		} else {
			AndroidGoogleMapsActivity.isLocationAvaliable = false;
			GBike.communicator.sendBTMsg("x");
		}
	}

	public Location getLocation() {
		return AndroidGoogleMapsActivity.gpsLocation;
	}

	public static void setMainActivity(GBike newMain) {
		firstActivity = newMain;
	}

	public static void setSelectionScreen(RouteSelector newMain) {
		placesOfInterestActivity = newMain;
	}

	public static void setMapsActivity(AndroidGoogleMapsActivity newMap) {
		mapViewActivity = newMap;
	}

}
